package com.controller;

import com.pojo.Course;
import com.pojo.R;
import com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseContoller {
    @Autowired
    private CourseService courseService;

    @RequestMapping("/allCourse")
    public R list() {
        List<Course> list = courseService.queryAllCourse();
        return R.ok(list);
    }

    @PostMapping("/addCourse")
    public R addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
        return R.ok();
    }

    @PostMapping("/del/{courseId}")
    public R deleteCourse(@PathVariable("courseId") Long id) {
        courseService.deleteCourseById(id);
        return R.ok();
    }

    @PostMapping("/updateCourse")
    public R update(@RequestBody Course course) {
        courseService.updateCourse(course);
        course = courseService.queryById(course.getCourseId());
        return R.ok(course);
    }

    @GetMapping("/getCourseById")
    public R getCourseById(@RequestParam("id") long id) {
        Course course = courseService.queryById(id);
        return R.ok(course);
    }
}
